package com.ruoyi.project.system.borrow.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.system.item.mapper.ItemMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.borrow.mapper.BorrowMapper;
import com.ruoyi.project.system.borrow.domain.Borrow;
import com.ruoyi.project.system.borrow.service.IBorrowService;
import com.ruoyi.common.utils.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 物品借阅Service业务层处理
 * 
 * @author stefan
 * @date 2021-11-10
 */
@Service
@Slf4j
public class BorrowServiceImpl implements IBorrowService 
{
    @Autowired
    private BorrowMapper borrowMapper;
    @Autowired
    private ItemMapper itemMapper;

    /**
     * 查询物品借阅
     * 
     * @param id 物品借阅主键
     * @return 物品借阅
     */
    @Override
    public Borrow selectBorrowById(String id)
    {
        return borrowMapper.selectBorrowById(id);
    }

    /**
     * 查询物品借阅列表
     * 
     * @param borrow 物品借阅
     * @return 物品借阅
     */
    @Override
    public List<Borrow> selectBorrowList(Borrow borrow)
    {
        return borrowMapper.selectBorrowList(borrow);
    }

    /**
     * 新增物品借阅
     * 
     * @param borrow 物品借阅
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBorrow(Borrow borrow)
    {
        borrow.setCreateTime(DateUtils.getNowDate());
        //设置节约中的物品状态为申请中
        borrow.setStatus(1);
        int i = borrowMapper.insertBorrow(borrow);
        try{
            itemMapper.updateItemStatusById(borrow.getItemId());
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return i;
    }

    /**
     * 修改物品借阅
     * 
     * @param borrow 物品借阅
     * @return 结果
     */
    @Override
    public int updateBorrow(Borrow borrow)
    {
        return borrowMapper.updateBorrow(borrow);
    }

    /**
     * 批量删除物品借阅
     * 
     * @param ids 需要删除的物品借阅主键
     * @return 结果
     */
    @Override
    public int deleteBorrowByIds(String ids)
    {
        return borrowMapper.deleteBorrowByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除物品借阅信息
     * 
     * @param id 物品借阅主键
     * @return 结果
     */
    @Override
    public int deleteBorrowById(String id)
    {
        return borrowMapper.deleteBorrowById(id);
    }

    /**
     * 查询审批物品列表
     *
     * @param borrow
     * @return
     */
    @Override
    public List<Borrow> selectBorrowPromiseList(Borrow borrow) {
        //获取borrow对象
        List<Borrow> borrows = borrowMapper.selectBorrowListAll();
        Long userId = ShiroUtils.getSysUser().getUserId();
        List<Borrow> promiseList = new ArrayList<>();
        try {
            for (Borrow borrow1 : borrows) {
                Integer status = borrow1.getStatus();
                Borrow borrow2 = borrowMapper.selectBorrowPromiseList(userId, status);
                promiseList.add(borrow2);
            }
            return promiseList;
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return null;

    }
}
